package com.jeecg.p3.base.util;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 时间工具类
 * @user : 张晓
 * @date : 2020-11-25
 */
public class DateUtil {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * Base ISO 8601 Date format yyyyMMdd i.e., 20021225 for the 25th day of December in the year 2002
	 */
	public static final String ISO_DATE_FORMAT = "yyyyMMdd";
	
	public static final String ISO_DATE_FORMAT_LAST = "yyyy-MM";

	/**
	 * Expanded ISO 8601 Date format yyyy-MM-dd i.e., 2002-12-25 for the 25th day of December in the year 2002
	 */
	public static final String ISO_EXPANDED_DATE_FORMAT = "yyyy-MM-dd";

	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * HH:mm
	 */
	public static String HOUR_MINUTE = "HH:mm";

	/**
	 * yyyy年MM月dd日
	 */
	public static final String CHINESE_EXPANDED_DATE_FORMAT = "yyyy年MM月dd日";

	/**
	 * Default lenient setting for getDate.
	 */
	private static boolean LENIENT_DATE = false;

	/**
	 * 暂时不用
	 * @param JD
	 * @return
	 */
	protected static final float normalizedJulian(float JD) {
		float f = Math.round(JD + 0.5f) - 0.5f;
		return f;
	}

	/**
	 * 比较传入的日期和当前日期
	 * @return boolean
	 */
	public static boolean compareDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String str = sdf.format(new Date());
		Calendar cal = Calendar.getInstance();
		Calendar cal1 = Calendar.getInstance();
		try {
			Date d1 = sdf.parse(date);
			Date d = sdf.parse(str);
			cal.setTime(d1);
			cal1.setTime(d);
			int result = cal.compareTo(cal1);
			if (result == 0) {
				return true;
			} else if (result == -1) {
				return false;
			} else if (result == 1) {
				return true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 浮点值转换成日期格式<br>
	 * 暂时不用
	 * Returns the Date from a julian. The Julian date will be converted to noon GMT,
	 * such that it matches the nearest half-Long (i.e., a julian date of 1.4 gets
	 * changed to 1.5, and 0.9 gets changed to 0.5.)
	 *
	 * @param JD the Julian date
	 * @return the Gregorian date
	 */
	public static final Date toDate(float JD) {

		/* To convert a Julian Day Number to a Gregorian date, assume that it is for 0 hours, Greenwich time (so
		 * that it ends in 0.5). Do the following calculations, again dropping the fractional part of all
		 * multiplicatons and divisions. Note: This method will not give dates accurately on the
		 * Gregorian Proleptic Calendar, i.e., the calendar you get by extending the Gregorian
		 * calendar backwards to years earlier than 1582. using the Gregorian leap year
		 * rules. In particular, the method fails if Y<400. */
		float Z = (normalizedJulian(JD)) + 0.5f;
		float W = (int) ((Z - 1867216.25f) / 36524.25f);
		float X = (int) (W / 4f);
		float A = Z + 1 + W - X;
		float B = A + 1524;
		float C = (int) ((B - 122.1) / 365.25);
		float D = (int) (365.25f * C);
		float E = (int) ((B - D) / 30.6001);
		float F = (int) (30.6001f * E);
		int day = (int) (B - D - F);
		int month = (int) (E - 1);

		if (month > 12) {
			month = month - 12;
		}

		int year = (int) (C - 4715); //(if Month is January or February) or C-4716 (otherwise)

		if (month > 2) {
			year--;
		}

		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1); // damn 0 offsets
		c.set(Calendar.DATE, day);

		return c.getTime();
	}

	/**
	 * 获取两个日期之间的天数
	 * Returns the days between two dates. Positive values indicate that
	 * the second date is after the first, and negative values indicate, well,
	 * the opposite. Relying on specific times is problematic.
	 *
	 * @param early the "first date"
	 * @param later the "second date"
	 * @return the days between the two dates
	 */
	public static final int daysBetween(Date early, Date later) {
		GregorianCalendar calst = new GregorianCalendar();
		GregorianCalendar caled = new GregorianCalendar();
		calst.setTime(early);
		caled.setTime(later);
		//设置时间为0时
		calst.set(GregorianCalendar.HOUR_OF_DAY, 0);
		calst.set(GregorianCalendar.MINUTE, 0);
		calst.set(GregorianCalendar.SECOND, 0);
		caled.set(GregorianCalendar.HOUR_OF_DAY, 0);
		caled.set(GregorianCalendar.MINUTE, 0);
		caled.set(GregorianCalendar.SECOND, 0);
		//得到两个日期相差的天数
		int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
		return days;
	}

	/**
	 * Returns the days between two dates. Positive values indicate that
	 * the second date is after the first, and negative values indicate, well,
	 * the opposite.
	 *
	 * @param early
	 * @param later
	 * @return the days between two dates.
	 */
	public static final int daysBetween(Calendar early, Calendar later) {
		return (int) (toJulian(later) - toJulian(early));
	}

	/**
	 * Return a Julian date based on the input parameter. This is
	 * based from calculations found at
	 * <a href="http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html">Julian Day Calculations
	 * (Gregorian Calendar)</a>, provided by Bill Jeffrys.
	 * @param c a calendar instance
	 * @return the julian day number
	 */
	public static final float toJulian(Calendar c) {
		int Y = c.get(Calendar.YEAR);
		int M = c.get(Calendar.MONTH);
		int D = c.get(Calendar.DATE);
		int A = Y / 100;
		int B = A / 4;
		int C = 2 - A + B;
		float E = (int) (365.25f * (Y + 4716));
		float F = (int) (30.6001f * (M + 1));
		float JD = C + D + E + F - 1524.5f;
		return JD;
	}

	/**
	 * 暂时不用
	 * Return a Julian date based on the input parameter. This is
	 * based from calculations found at
	 * <a href="http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html">Julian Day Calculations
	 * (Gregorian Calendar)</a>, provided by Bill Jeffrys.
	 * @param date
	 * @return the julian day number
	 */
	public static final float toJulian(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return toJulian(c);
	}

	/**
	 * 日期增加
	 * @param isoString 日期字符串
	 * @param fmt 格式
	 * @param field 年/月/日 Calendar.YEAR/Calendar.MONTH/Calendar.DATE
	 * @param amount 增加数量
	 * @return
	 * @throws ParseException
	 */
	public static final String dateIncrease(String isoString, String fmt, int field, int amount) {
		try {
			Calendar cal = GregorianCalendar.getInstance();
			cal.setTime(stringToDate(isoString, fmt, true));
			cal.add(field, amount);
			return dateToString(cal.getTime(), fmt);
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * Time Field Rolling function.
	 * Rolls (up/down) a single unit of time on the given time field.
	 *
	 * @param isoString
	 * @param field the time field.
	 * @param up Indicates if rolling up or rolling down the field value.
	 * @exception ParseException if an unknown field value is given.
	 */
	public static final String roll(String isoString, String fmt, int field, boolean up) throws ParseException {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(stringToDate(isoString, fmt));
		cal.roll(field, up);
		return dateToString(cal.getTime(), fmt);
	}

	/**
	 * Time Field Rolling function.
	 * Rolls (up/down) a single unit of time on the given time field.
	 *
	 * @param isoString
	 * @param field the time field.
	 * @param up Indicates if rolling up or rolling down the field value.
	 * @exception ParseException if an unknown field value is given.
	 */
	public static final String roll(String isoString, int field, boolean up) throws ParseException {
		return roll(isoString, DATETIME_PATTERN, field, up);
	}

	/**
	 * 字符串转换为日期java.util.Date
	 * @param dateText 字符串
	 * @param format 日期格式
	 * @param lenient 日期越界标志
	 * @return
	 */
	public static Date stringToDate(String dateText, String format, boolean lenient) {
		if (dateText == null) {
			return null;
		}
		DateFormat df = null;
		try {
			if (format == null) {
				df = new SimpleDateFormat();
			} else {
				df = new SimpleDateFormat(format);
			}
			// setLenient avoids allowing dates like 9/32/2001
			// which would otherwise parse to 10/2/2001
			df.setLenient(false);
			return df.parse(dateText);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 字符串转换为日期java.util.Date
	 * @param dateString 字符串
	 * @param format 日期格式
	 * @return
	 */
	public static Date stringToDate(String dateString, String format) {
		return stringToDate(dateString, format, LENIENT_DATE);
	}

	/**
	 * 字符串转换为日期java.util.Date
	 * @param dateString 字符串
	 */
	public static Date stringToDate(String dateString) {
		return stringToDate(dateString, ISO_EXPANDED_DATE_FORMAT, LENIENT_DATE);
	}

	/** 根据时间变量返回时间字符串
	 * @return 返回时间字符串
	 * @param pattern 时间字符串样式
	 * @param date 时间变量
	 */
	public static String dateToString(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		try {
			SimpleDateFormat sfDate = new SimpleDateFormat(pattern);
			sfDate.setLenient(false);
			return sfDate.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * @deprecated
	 */
	public static String dateToStringEnd(Date date, String pattern) {
		return DateUtil.dateToString(date, DateUtil.ISO_EXPANDED_DATE_FORMAT) + " 23:59:59.000";
	}

	/**
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * @deprecated
	 */
	public static String dateToStringBegin(Date date, String pattern) {
		return DateUtil.dateToString(date, DateUtil.ISO_EXPANDED_DATE_FORMAT) + " 00:00:00.000";
	}

	/**
	 * 根据时间变量返回时间字符串 yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date) {
		return dateToString(date, ISO_EXPANDED_DATE_FORMAT);
	}

	/** 返回当前时间
	 * @return 返回当前时间
	 */
	public static Date getCurrentDateTime() {
		Calendar calNow = Calendar.getInstance();
		Date dtNow = calNow.getTime();

		return dtNow;
	}

	/**
	 * 返回当前日期字符串
	 * @param pattern 日期字符串样式
	 * @return
	 */
	public static String getCurrentDateString(String pattern) {
		return dateToString(getCurrentDateTime(), pattern);
	}

	/**
	 * 返回当前日期字符串 yyyy-MM-dd
	 * @return
	 */
	public static String getCurrentDateString() {
		return dateToString(getCurrentDateTime(), ISO_EXPANDED_DATE_FORMAT);
	}

	/**
	 * 返回当前日期+时间字符串 yyyy-MM-dd hh:mm:ss
	 * @param date
	 * @return
	 */
	public static String dateToStringWithTime(Date date) {
		return dateToString(date, DATETIME_PATTERN);
	}

	/**
	 * 日期增加-按日增加
	 * @param date
	 * @param days
	 * @return java.util.Date
	 */
	public static Date dateIncreaseByDay(Date date, int days) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, days);
		return cal.getTime();
	}

	/**
	 * 日期增加-按月增加
	 * @param date
	 * @param mnt
	 * @return java.util.Date
	 */
	public static Date dateIncreaseByMonth(Date date, int mnt) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, mnt);
		return cal.getTime();
	}

	/**
	 * 日期增加-按年增加
	 * @param date
	 * @param mnt
	 * @return java.util.Date
	 */
	public static Date dateIncreaseByYear(Date date, int mnt) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, mnt);
		return cal.getTime();
	}

	/**
	 * 日期增加
	 * @param date 日期字符串 yyyy-MM-dd
	 * @param days
	 * @return 日期字符串 yyyy-MM-dd
	 */
	public static String dateIncreaseByDay(String date, int days) {
		return dateIncreaseByDay(date, ISO_DATE_FORMAT, days);
	}

	/**
	 * 日期增加
	 * @param date 日期字符串
	 * @param fmt 日期格式
	 * @param days
	 * @return
	 */
	public static String dateIncreaseByDay(String date, String fmt, int days) {
		return dateIncrease(date, fmt, Calendar.DATE, days);
	}

	/**
	 * 取得当前日期所在月的最后一天
	 * @param yearM
	 * @return
	 */
	public static Date getLastDayInMonth(String yearM) {
		String yymmdd = yearM + "01";
		Date date = DateUtil.stringToDate(yymmdd, ISO_DATE_FORMAT);
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DATE, -1);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 23);
		cal.set(GregorianCalendar.MINUTE, 59);
		cal.set(GregorianCalendar.SECOND, 59);
		cal.set(GregorianCalendar.MILLISECOND, 0);
		return cal.getTime();
	}
	/**
	 * 取得某日期所在月的最后一天
	 * @comments 
	 * @param yearM yyyy-MM
	 * @return
	 * @version 1.0
	 */
	public static Date getLastDayFromMonth(String yearM){
		String yymmdd = yearM + "-01";
		Date date = DateUtil.stringToDate(yymmdd, ISO_EXPANDED_DATE_FORMAT);
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DATE, -1);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 23);
		cal.set(GregorianCalendar.MINUTE, 59);
		cal.set(GregorianCalendar.SECOND, 59);
		cal.set(GregorianCalendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	/**
	 * 取得当前日期所在月的最后一天
	 * @param date
	 * @return
	 */
	public static Date getLastDayInMonth(Date date) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(stringToDate(dateToString(date, "yyyy-MM") + "-01"));
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DATE, -1);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 23);
		cal.set(GregorianCalendar.MINUTE, 59);
		cal.set(GregorianCalendar.SECOND, 59);
		cal.set(GregorianCalendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 取得当前日期在一个月的第几天
	 * @param date
	 * @return
	 */
	public static int getDaysInMonth(Date date) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 取得当前日期在月的最后一天。即总天数
	 * @param date
	 * @return
	 */
	public static int getAllDaysInMonth(Date date) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);

		return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 取得当前日期所在月的第一天
	 * @param yearM
	 * @return
	 */
	public static Date getFirstDayInMonth(String yearM) {
		String yymmdd = yearM + "01";
		Date date = DateUtil.stringToDate(yymmdd, ISO_DATE_FORMAT);
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);
		return cal.getTime();
	}
	/**
	 * 取得某日期所在月的第一天
	 * @comments 
	 * @param dStr 2015-02
	 * @return
	 * @version 1.0
	 */
	public static Date getFirstDayFromMonth(String dStr){
		String dateStr = dStr + "-01";
		Date date = DateUtil.stringToDate(dateStr, ISO_EXPANDED_DATE_FORMAT);
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);
		return cal.getTime();
		
	}
	
	/**
	 * 取得当前日期所在月的第一天
	 * @param date
	 * @return
	 */
	public static Date getFirstDayInMonth(Date date) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);
		cal.set(GregorianCalendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 取得当前日期所在月的第一天。
	 * @user : WuZheng
	 * @date : 2012-7-18
	 * @param year 年份
	 * @param month 月份
	 * @return
	 */
	public static Date getFirstDayInMonth(Integer year, Integer month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 取得当前日期所在月的第一天
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @param date
	 * @return
	 */
	public static String getYearMonth(Date date) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);
		String lastMonth = dateToString(cal.getTime(), ISO_DATE_FORMAT);
		return lastMonth.substring(0, lastMonth.length() - 2);
	}

	/**
	 * 取得当前月的上个月。
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @return
	 */
	public static String getLastMonthInToday() {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.MONTH, -1);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);

		String lastMonth = dateToString(cal.getTime(), ISO_DATE_FORMAT);

		return lastMonth.substring(0, lastMonth.length() - 2);
	}
	/**
	 * 取得当前月的上个月。格式：yyyy-MM
	 * @user : wangyd
	 * @date : 2015-3
	 * @return
	 */
	public static String getLastMonthIn_Today() {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.MONTH, -1);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);

		String lastMonth = dateToString(cal.getTime(), ISO_DATE_FORMAT_LAST);

		return lastMonth;
	}

	/**
	 * 取得指定月的上个月。格式为：yyyyMM
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @param yearM
	 * @return
	 */
	public static String getLastMonth(String yearM) {
		String yymmdd = yearM + "01";
		Date date = DateUtil.stringToDate(yymmdd, ISO_DATE_FORMAT);
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.MONTH, -1);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);

		String lastMonth = dateToString(cal.getTime(), ISO_DATE_FORMAT);

		return lastMonth.substring(0, lastMonth.length() - 2);
	}

	/**
	 * 取得指定月的下个月。  格式为：yyyy-MM
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @param year_M
	 * @return
	 */
	public static String getAfter_Month(String year_M) {
		String yymmdd = year_M + "-01";
		Date date = DateUtil.stringToDate(yymmdd, ISO_EXPANDED_DATE_FORMAT);
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.MONTH, 1);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);

		String lastMonth = dateToString(cal.getTime(), ISO_EXPANDED_DATE_FORMAT);

		return lastMonth.substring(0, lastMonth.length() - 3);
	}

	/**
	 * 取得指定月的上个月。格式为：yyyy-MM
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @param year_M
	 * @return
	 */
	public static String getLast_Month(String year_M) {
		String yymmdd = year_M + "-01";
		Date date = DateUtil.stringToDate(yymmdd, ISO_EXPANDED_DATE_FORMAT);
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.MONTH, -1);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);

		String lastMonth = dateToString(cal.getTime(), ISO_EXPANDED_DATE_FORMAT);

		return lastMonth.substring(0, lastMonth.length() - 3);
	}

	/**
	 * 取得指定月的前第十二个月。
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @param yearM
	 * @return
	 */
	public static String getBeforTwelveMonth(String yearM) {
		String yymmdd = yearM + "01";
		Date date = DateUtil.stringToDate(yymmdd, ISO_DATE_FORMAT);
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.MONTH, -12);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);

		String lastMonth = dateToString(cal.getTime(), ISO_DATE_FORMAT);

		return lastMonth.substring(0, lastMonth.length() - 2);
	}

	/**
	 * 取得指定月的前第六个月(前半年)。
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @param yearM
	 * @return
	 */
	public static String getBeforSixMonth(String yearM) {
		String yymmdd = yearM + "01";
		Date date = DateUtil.stringToDate(yymmdd, ISO_DATE_FORMAT);
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.MONTH, -6);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);
		cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
		cal.set(GregorianCalendar.MINUTE, 0);
		cal.set(GregorianCalendar.SECOND, 0);

		String lastMonth = dateToString(cal.getTime(), ISO_DATE_FORMAT);

		return lastMonth.substring(0, lastMonth.length() - 2);
	}

	/**
	 * 获取当前年月及当前年月的上个月集合
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @return
	 */
	public static List<String> getCurrentAndLastMonth() {
		List<String> yearMs = new ArrayList<String>();
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(new Date());

		String yearM = dateToString(cal.getTime(), ISO_DATE_FORMAT);
		yearM = yearM.substring(0, yearM.length() - 2);
		yearMs.add(yearM);

		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.MONTH, -1);
		yearM = dateToString(cal.getTime(), ISO_DATE_FORMAT);
		yearM = yearM.substring(0, yearM.length() - 2);
		yearMs.add(yearM);

		return yearMs;
	}

	/**
	 * 获取当前年月
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @return
	 */
	public static String getCurentMonth() {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(new Date());

		String yearM = dateToString(cal.getTime(), ISO_DATE_FORMAT);
		yearM = yearM.substring(0, yearM.length() - 2);

		return yearM;
	}

	/**
	 * 日期字符串格式转换
	 * @param src 日期字符串
	 * @param srcfmt 源日期格式
	 * @param desfmt 目标日期格式
	 * @return
	 */
	public static String stringToString(String src, String srcfmt, String desfmt) {
		return dateToString(stringToDate(src, srcfmt), desfmt);
	}

	public static String getCurrentYearWeek() {
		Calendar cal = Calendar.getInstance();
		String rtn = String.format("%4d%2d", cal.get(Calendar.YEAR), cal.get(Calendar.WEEK_OF_YEAR));
		return rtn;
	}

	/**
	 * 两个时间对象的比较
	 * @param first the "first date"
	 * @param second the "second date"
	 * @return 
	 * 0:	first = second
	 * <0: 	first < second
	 * >0:  first > second
	 */
	public static final long compare(Date first, Date second) {
		if (first == null && second == null) {
			return 0;
		} else if (first == null) {
			return -1;
		} else if (second == null) {
			return 1;
		}

		GregorianCalendar calst = new GregorianCalendar();
		GregorianCalendar caled = new GregorianCalendar();
		calst.setTime(first);
		caled.setTime(second);
		//设置时间为0时
		calst.set(GregorianCalendar.HOUR_OF_DAY, 0);
		calst.set(GregorianCalendar.MINUTE, 0);
		calst.set(GregorianCalendar.SECOND, 0);
		caled.set(GregorianCalendar.HOUR_OF_DAY, 0);
		caled.set(GregorianCalendar.MINUTE, 0);
		caled.set(GregorianCalendar.SECOND, 0);

		long start = calst.get(GregorianCalendar.YEAR) * 10000 + calst.get(GregorianCalendar.MONTH) * 100
				+ calst.get(GregorianCalendar.DAY_OF_MONTH);
		long end = caled.get(GregorianCalendar.YEAR) * 10000 + caled.get(GregorianCalendar.MONTH) * 100
				+ caled.get(GregorianCalendar.DAY_OF_MONTH);
		return start - end;
	}

	/**
	 * 两个日期的小时差
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static final BigDecimal hoursBetween(Date startDate, Date endDate, int scale) {
		Calendar sc = Calendar.getInstance();
		sc.setTime(startDate);

		Calendar ec = Calendar.getInstance();
		ec.setTime(endDate);

		Long date = ec.getTimeInMillis() - sc.getTimeInMillis();
		BigDecimal result = new BigDecimal(date.toString());
		BigDecimal unit = new BigDecimal("3600000");

		result = result.divide(unit, scale, BigDecimal.ROUND_HALF_EVEN);

		return result;
	}

	/**
	 * 日期是否为空
	 * @param date
	 * @return
	 */
	public static final boolean isEmpty(Date date) {
		return (date == null) ? true : date.toString().length() < 1;
	}

	/**
	 * 夏令时判断
	 * @return
	 */
	public static boolean isDaylightSavingTime() {
		Date nowDate = new Date();

		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.OCTOBER), c.getActualMinimum(Calendar.DAY_OF_MONTH));

		Calendar c2 = Calendar.getInstance();
		c2.set(c2.get(Calendar.YEAR), c2.get(Calendar.MAY), c2.getActualMinimum(Calendar.DAY_OF_MONTH));

		return nowDate.after(c2.getTime()) && nowDate.before(c.getTime());
	}

	/**
	 * 夏令时/非夏令时 中午休息开始时间
	 * @param date
	 * @return
	 */
	public static Date getNoonRestStartTime(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		if (isDaylightSavingTime()) {
			c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 12, 00, 00);
		} else {
			c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 12, 00, 00);
		}

		return c.getTime();
	}

	/**
	 * 夏令时/非夏令时 下午上班开始时间
	 * @param date
	 * @return
	 */
	public static Date getAfterNoonWorkStartTime(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		if (isDaylightSavingTime()) {
			c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 14, 00, 00);
		} else {
			c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 13, 30, 00);
		}

		return c.getTime();
	}

	/**
	 * 夏令时/非夏令时 午休小时数
	 * @return
	 */
	public static BigDecimal getNoonRestHours() {
		if (isDaylightSavingTime()) {
			return new BigDecimal("2");
		} else {
			return new BigDecimal("1.5");
		}
	}

	/**
	 * 夏令时/非夏令时 下午下班时间
	 * @param date
	 * @return
	 */
	public static Date getAfterNoonWorkEndTime(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		if (isDaylightSavingTime()) {
			c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 18, 00, 00);
		} else {
			c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 17, 30, 00);
		}

		return c.getTime();
	}

	/**
	 * 夏令时/非夏令时 上午上班开始时间
	 * @param date
	 * @return
	 */
	public static Date getNoonWorkStartTime(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		if (isDaylightSavingTime()) {
			c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 8, 00, 00);
		} else {
			c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 8, 00, 00);
		}

		return c.getTime();
	}

	/**
	 * 时间比较 (yyyy-MM-dd)
	 * 等于0:时间相等，小于0:src日期在tgt之前,大于于0:src日期在tgt之后
	 * @param srcDate 
	 * @param tgtDate 
	 * @return
	 */
	public static long cmp(Date srcDate, Date tgtDate) {
		if (srcDate == null && tgtDate == null) {
			return 0;
		} else if (srcDate == null) {
			return -1;
		} else if (tgtDate == null) {
			return 1;
		}

		Calendar lhs = Calendar.getInstance();
		lhs.setTime(srcDate);
		Calendar rhs = Calendar.getInstance();
		rhs.setTime(tgtDate);

		long start = lhs.get(Calendar.YEAR) * 10000 + lhs.get(Calendar.MONTH) * 100 + lhs.get(Calendar.DAY_OF_MONTH);
		long end = rhs.get(Calendar.YEAR) * 10000 + rhs.get(Calendar.MONTH) * 100 + rhs.get(Calendar.DAY_OF_MONTH);
		return start - end;

	}

	/**
	 * 计算计算两个日期之间的年数
	 * 
	 * @param early
	 * @param late
	 * @return
	 */
	public static final int yearBetween(Date early, Date late) {
		if (early == null || late == null) {
			return 0;
		}
		int years = 0;
		// 计算两个日期之间的年数
		Calendar cEarly = Calendar.getInstance();
		cEarly.setTime(early);
		Calendar cLate = Calendar.getInstance();
		cLate.setTime(late);
		years = cLate.get(Calendar.YEAR) - cEarly.get(Calendar.YEAR);
		// 矫正月、日
		cEarly.add(Calendar.YEAR, years);
		if (cEarly.after(cLate)) {
			years--;
		}
		return years;
	}

	/**
	 * 获得入司月份
	 * @param enterDate
	 * @return
	 */
	public static Integer calculateMonthEnterInComp(Date enterDate) {
		if (enterDate == null) {
			return 0;
		}
		Calendar calEnterDate = Calendar.getInstance();
		calEnterDate.setTime(enterDate);

		Date currentDate = new Date();
		Calendar paybaseDate = Calendar.getInstance();
		paybaseDate.setTime(currentDate);
		paybaseDate.set(Calendar.MONTH, Calendar.APRIL);

		int years = calEnterDate.get(Calendar.YEAR) - paybaseDate.get(Calendar.YEAR);

		int number = 12 * years + (calEnterDate.get(Calendar.MONTH) - paybaseDate.get(Calendar.MONTH));

		return number;
	}

	/**
	 * 计算中间的月份
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @param from
	 * @param to
	 * @return
	 */
	public static int monthBetween(Date from, Date to) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(to);
		int months = cal.get(Calendar.YEAR) * 12 + cal.get(Calendar.MONTH);

		cal.setTime(from);
		months -= cal.get(Calendar.YEAR) * 12 + cal.get(Calendar.MONTH);

		return months;
	}

	/**
	 * 比较两个日期是否相同
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static boolean isSomeMonth(Date startDate, Date endDate) {
		Calendar cal = Calendar.getInstance();

		cal.setTime(startDate);
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0);

		Date sd = cal.getTime();

		cal.setTime(endDate);
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0);

		Date ed = cal.getTime();

		return cmp(sd, ed) == 0;

	}

	/**
	 * 返回当前月的前N个月
	 * @user : wangyadong
	 * @date : 2012-6-6
	 * @param n
	 * @return List<String>
	 */
	public static List<String> getFirstNMonths(Integer n) {
		List<String> monthList = new ArrayList<String>();
		int i = 0;
		while (i < n) {
			if (i == 0) {
				monthList.add(DateUtil.getLastMonth(DateUtil.dateToString(new Date(), "yyyyMM")));
			} else {
				monthList.add(DateUtil.getLastMonth(monthList.get(i - 1)));
			}
			i++;
		}
		return monthList;
	}

	/**
	 * 返回指定月的前N个月
	 * @user : Administrator
	 * @date : 2012-6-7
	 * @param date 格式为yyyyMM
	 * @param n
	 * @return List<String>
	 */
	public static List<String> getSpecifiedFirstNMonths(String date, Integer n) {
		List<String> monthList = new ArrayList<String>();
		int i = 0;
		while (i < n) {
			if (i == 0) {
				monthList.add(DateUtil.getLastMonth(date));
			} else {
				monthList.add(DateUtil.getLastMonth(monthList.get(i - 1)));
			}
			i++;
		}
		return monthList;
	}

	/**比较两个日期大小
	 * @param format 格式
	 * @param date2 日期2
	 * @param date1 日期1
	 * @throws Exception 
	 * @user : jerry
	 * @date : 2012-6-15
	 */
	public static int compareDate(String date1, String date2, String format) throws Exception {
		try {
			DateFormat df = new SimpleDateFormat(format);
			Date dt1 = df.parse(date1);
			Date dt2 = df.parse(date2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
			throw exception;
		}
	}

	/**
	 * 返回两个Date对象中表示时间较大的Date对象。
	 * @user : WuZheng
	 * @date : 2012-7-3
	 * @param dt1 Date对象
	 * @param dt2 Date对象
	 * @return 较大的Date对象
	 */
	public static Date max(Date dt1, Date dt2) {
		return compare(dt1, dt2) >= 0 ? dt1 : dt2;
	}

	/**
	 * 得到Date对象所在的季度
	 * @user : jerry
	 * @date : 2012-7-30
	 * @param date 日期
	 * @return
	 */
	public static int getQuarter(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int month = cal.get(Calendar.MONTH);
		return (month + 3) / 3;
	}

	/**
	 * 得到本月的第一天
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @param syear 当前年
	 * @param smonth 当前月
	 * @return
	 */
	public static String getFirstDayByMonth(String syear, String smonth) {
		Integer year = new Integer(syear);
		Integer month = new Integer(smonth);
		return year + "-" + month + "-1";
	}

	/**
	 * 得到下个月的第一天
	 * @user : wangyd
	 * @date : 2015-3-4
	 * @param syear 当前年
	 * @param smonth 当前月
	 * @return
	 */
	public static String getNextMonthFirstDayByMonth(String syear, String smonth) {
		Integer year = new Integer(syear);
		Integer month = new Integer(smonth);
		Integer nextYear = year;
		Integer nextMonth = month + 1;
		if (month == 12) {
			nextYear = year + 1;
			nextMonth = 1;
		}
		return nextYear + "-" + nextMonth + "-1";
	}

	public static void main(String[] args) {
		//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//String lastDayOfMonth = sdf.format(getLastDayByMonth("2020", "12").getTime());
		System.out.println(getLastMonthInToday());
		System.out.println(dateToString(getLastDayInMonth(getLastMonthInToday())));
		System.out.println(dateToString(getFirstDayFromMonth(dateToString(getLastDayInMonth(getLastMonthInToday())))));

		String nowMonth = DateUtil.dateToString(new Date(), "yyyyMM");
		String nowDate = DateUtil.dateToString(new Date());
		String nowStartDate = DateUtil.dateToString(DateUtil.getFirstDayFromMonth(nowDate));
		System.out.println(nowMonth);
		System.out.println(nowDate);
		System.out.println(nowStartDate);
	}
	/**
	 * 取得当前日期所在月的最后一天
	 * @param syear
	 * @param smonth
	 * @return
	 */
	public static Calendar getLastDayByMonth(String syear, String smonth) {
		String sdate = syear + "-" + smonth + "-1";
		Date date = DateUtil.stringToDate(sdate, ISO_EXPANDED_DATE_FORMAT);
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DATE, -1);
		//设置时间为0时
		cal.set(GregorianCalendar.HOUR_OF_DAY, 23);
		cal.set(GregorianCalendar.MINUTE, 59);
		cal.set(GregorianCalendar.SECOND, 59);
		cal.set(GregorianCalendar.MILLISECOND, 0);
		return cal;
	}

	/**
	 * 取得两个日期之间的双休天数
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static long getNonWorkingDays(String beginDate, String endDate) {
		Date st = stringToDate(beginDate, ISO_EXPANDED_DATE_FORMAT);
		Date et = stringToDate(endDate, ISO_EXPANDED_DATE_FORMAT);

		//Calendar cal1 = Calendar.getInstance();
		//cal1.setTime(st);
		//Calendar cal2 = Calendar.getInstance();
		//cal2.setTime(et);

		//long day = 86400000;//一天的millis
		//long mod =(cal2.getTimeInMillis()-cal1.getTimeInMillis())/day;
		//int n = cal1.get(Calendar.DAY_OF_WEEK);//开始日期是周几
		//long cnt = mod/7;//几个整周
		//cnt=cnt*2;		 //整周的双休天数
		//long yushu = mod%7;

		//Calendar cal1 = Calendar.getInstance();
		//cal1.setTime(st);
		//Calendar cal2 = Calendar.getInstance();
		//cal2.setTime(et);

		Calendar cal = Calendar.getInstance();
		cal.setTime(st);
		long mod = daysBetween(st, et);
		int n = cal.get(Calendar.DAY_OF_WEEK);//开始日期是周几
		long cnt = mod / 7;//几个整周
		cnt = cnt * 2; //整周的双休天数
		long yushu = mod % 7;
		if (n + yushu > 7)
			cnt = cnt + 2;//过了周六
		if (n + yushu == 7)
			cnt++;//正好是周六
		return cnt;
	}

	/**
	 * 取得两个日期之间的双休天数
	 * @param stdt
	 * @param etdt
	 * @return
	 */
	public static long getNonWorkingDays(Date stdt, Date etdt) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(stdt);
		long mod = daysBetween(stdt, etdt);
		int n = cal.get(Calendar.DAY_OF_WEEK);//开始日期是周几
		long cnt = mod / 7;//几个整周
		cnt = cnt * 2; //整周的双休天数
		long yushu = mod % 7;
		if (n + yushu > 7)
			cnt = cnt + 2;//过了周六
		if (n + yushu == 7)
			cnt++;//正好是周六
		return cnt;
	}

	/**
	 * 取得指定月份的所有日期
	 * @user : jerry
	 * @date : 2013-7-5
	 * @param year
	 * @param month
	 * @return
	 */
	public static List<Date> getDaysOfMonth(Integer year, Integer month) {
		List<Date> result = new ArrayList<Date>();
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);

		int days = getAllDaysInMonth(cal.getTime());

		for (int i = 1; i <= days; i++) {
			cal.set(Calendar.DAY_OF_MONTH, i);
			result.add(cal.getTime());
		}

		return result;
	}

	/**
	 * 根据指定日期返回前一天日期
	 * @user : wangyd
	 * @date : 2014-11-8
	 * @param todayDate
	 * @throws ParseException
	 */
	public static String getBeforeDate(String todayDate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(todayDate));
		int day = cal.get(Calendar.DATE);
		cal.set(Calendar.DATE, day - 1);
		return sdf.format(cal.getTime());
	}

	/**
	 * 获取自定义格式化日期
	 * @user : wangyd
	 * @date : 2014-11-8
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getFormatDate(Date date, String pattern) {
		if(date != null){
			sdf.applyPattern(pattern);
			return sdf.format(date);
		}
		return "";
	}

	/**
	 * 将日期转换为date
	 * @user : wangyd
	 * @date : 2014-11-25
	 * @param datestr
	 * @return
	 */
	public static Date getParseDate(String datestr) {
		try {
			return sdf.parse(datestr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}

	/**
	 * Sql返回Timestamp 转换为 string
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String covertTimestampToStr(Timestamp date, String pattern) {
		sdf.applyPattern(pattern);
		try {
			return sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 格式化查询条件中的时间 追加00:00:00 或 23:59:59
	 * @user : wangyd
	 * @date : 2014-12-24
	 * @param date 格式必须为yyyy-MM-dd 
	 * @param isStart 开始时间为true，结束时间为false
	 * @return
	 */
	public static String formatStartEndTime(String date, Boolean isStart) {
		if (isStart) {
			return date + " 00:00:00";
		} else {
			return date + " 23:59:59";
		}
	}
	/**
	 * 获取某月天数
	 * @comments 
	 * @param date
	 * @return
	 * @version 1.0
	 */
	public static int getDaysOfTheMonth(Date date){
		Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date); 
		int days = rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);
		return days;
	}
	/**
	 * 某月有几个周日
	 * @comments 
	 * @param date
	 * @return
	 * @version 1.0
	 */
	public static int getWeekdays(Date date,String weekday) {
		int weekdays = 0;
		String weekdayTemp = "";
		SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
		Calendar setDate = Calendar.getInstance();
		setDate.setTime(date);
		if(weekday.equals("1")){
			weekdayTemp = "星期一";
		}else if(weekday.equals("2")){
			weekdayTemp = "星期二";
		}else if(weekday.equals("3")){
			weekdayTemp = "星期三";
		}else if(weekday.equals("4")){
			weekdayTemp = "星期四";
		}else if(weekday.equals("5")){
			weekdayTemp = "星期五";
		}else if(weekday.equals("6")){
			weekdayTemp = "星期六";
		}else if(weekday.equals("0")){
			weekdayTemp = "星期日";
		}
		// 从第一天开始
		String str = "";
		int day;
		for (day = 1; day <= getDaysOfTheMonth(date); day++) {
			if(day != 1){
				setDate.add(Calendar.DATE, 1);
				str = sdf.format(setDate.getTime());
			}else{
				str = sdf.format(setDate.getTime());
			}
			if (str.equals(weekdayTemp)) {
				weekdays++;
			}
		}
		return weekdays;
	}
	
	/**
	 * 通过数字下标获得汉字星期（0-6分别为周日到周六）
	 * @user : wangyd
	 * @date : 2015-3-6
	 * @param num
	 * @return
	 */
	public static String numToWeek(int num) {
		if(num>6){
			return null;
		}
		String[] weeks = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		return weeks[num];
	}
	
	/**
	 * @Comments 获取时间 时，分（HH:mm）
	 * @user : heshaolong
	 * @date : 2015-3-9
	 * @param date 日期
	 * @return 返回日期格式的时间（HH:mm）
	 * @version 1.0
	 */
	public static Date getHourMinute(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		Date time = null;
		try {
			time = sdf.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}
	/**
	 * 计算两个日期之间差天数
	 * @comments 
	 * @param startDate 起始时间
	 * @param endDate 结束时间
	 * @return 差天数
	 * @throws ParseException
	 * @version 1.0
	 */
	public static int daysBetween(String startDate,String endDate) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat(ISO_EXPANDED_DATE_FORMAT);  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(sdf.parse(startDate));
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(sdf.parse(endDate));    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));   
	}
	
	
	/**
	 * 获取日期的时分秒
	 * @comments 
	 * @param dateStr String类型的日期
	 * @return
	 * @throws ParseException
	 * @version 1.0
	 */
	public static String getHoursMinutesSeconds(String dateStr) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");  
		Date date = sdf.parse(dateStr);
		return sdf.format(date);
	}
	/**
	 * 把日期类型的时分秒转成字符串时分秒
	 * @comments 
	 * @param date
	 * @return
	 * @version 1.0
	 */
	public static String getHoursMinutesSeconds(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 
		return sdf.format(date);
	}
	/**
	 * 比较两个时间的大小
	 * @comments 
	 * @param minT HH:mm:ss
	 * @param maxT HH:mm:ss
	 * @return
	 * @version 1.0
	 */
	public static int compareTime(String minT,String maxT){
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		try {
			Date dt1 = sdf.parse(minT);
			Date dt2 = sdf.parse(maxT);
			if(dt1.getTime() > dt2.getTime()){
				return -1;
			}else if(dt1.getTime() < dt2.getTime()){
				return 1;
			}else{
				return 0;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	/**
	 * 获取某日期是该月的第几天 (2013-03-2 是 第二天)
	* @Description: 
	* @param @param date Date类型
	* @param @return
	* @return int
	*/
	public static int getDayOfMonth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
	
	/**
	 * 计算两个日期直接的日期集合
	 * @user : heshaolong
	 * @date : 2015-3-11
	 * @param startDate
	 * @param endDate
	 * @param list
	 * @return
	 * @version : 1.0
	 */
	public static List<String> getBetweenTwoDateList(Date startDate, Date endDate,List<String> list){
		try {
			if(startDate == null || endDate == null) {
				return null;
			}
			String startDateStr = dateToString(startDate,"yyyy-MM-dd HH:mm:ss");
			String endDateStr = dateToString(endDate,"yyyy-MM-dd HH:mm:ss");
			if(list == null){
				list = new ArrayList<String>();
				list.add(startDateStr);
				if(daysBetween(startDateStr,endDateStr) == 0){
					list.add(endDateStr);
				}
			}else{
				if(daysBetween(startDateStr,endDateStr) == 0){
					list.add(endDateStr);
				}else if(daysBetween(startDateStr,endDateStr) > 0){
					list.add(startDateStr);
				}
			}
			if(daysBetween(startDateStr,endDateStr) > 0){
				Calendar cal = Calendar.getInstance();
				cal.setTime(startDate);
				cal.add(Calendar.DATE, 1);
				getBetweenTwoDateList(cal.getTime(), endDate,list);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 
	 * @Title: isSunday
	 * @Description: 判断是周天
	 * @author zx
	 * @return: Boolean
	 */
	public static Boolean isSunday() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @Title: isSaturday
	 * @Description: 判断是周六
	 * @author zx
	 * @return: Boolean
	 */
	public static Boolean isSaturday() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
			return true;
		}
		return false;
	}

	/**
	 * 获取过去第几天的日期
	 *
	 * @param past
	 * @return
	 */
	public static String getPastDate(int past) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
		Date today = calendar.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String result = format.format(today);
		return result;
	}

	/**
	 * 获取未来 第 past 天的日期
	 * @param past
	 * @return
	 */
	public static String getFetureDate(int past) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
		Date today = calendar.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String result = format.format(today);
		return result;
	}
}
